package com.qf.hospital.system.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.hospital.system.entity.SysMenu;
import com.qf.hospital.system.entity.SysRole;
import com.qf.hospital.system.mapper.SysRoleMapper;
import com.qf.hospital.system.service.SysRoleService;
import com.qf.hospital.system.vo.DTreeVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    public PageInfo<SysRole> findByConditionAndPage(SysRole parameter, Integer page, Integer limit) {
        //1. 分页
        PageHelper.startPage(page,limit);
        //2. 查询
        List<SysRole> list = sysRoleMapper.findAllRole(parameter);
        //3. 封装
        PageInfo<SysRole> pageInfo = new PageInfo<>(list);
        //4. 返回
        return pageInfo;
    }

    @Override
    public List<DTreeVO> initRoleMenuTreeJson(Integer roleId) {
        //1. 查询到全部的菜单功能
        List<DTreeVO> result = sysRoleMapper.findAllMenuAsDTreeVO();

        //2. 查询当前角色拥有的菜单功能
        List<SysMenu> list = sysRoleMapper.findRoleMenusByRoleId(roleId);

        //3. 遍历全部的菜单功能,封装checkArr
        for (DTreeVO dTreeVO : result) {
            Integer id = dTreeVO.getId();
            dTreeVO.setCheckArr("0");
            // 遍历list做对比
            for (SysMenu sysMenu : list) {
                if(sysMenu.getId() == id){
                    // 角色拥有这个菜单功能
                    dTreeVO.setCheckArr("1");
                    break;
                }
            }
        }

        //4. 返回
        return result;
    }
}
